<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <jsp:include page="/WEB-INF/views/admin/common/import.jsp">
        <jsp:param name="title" value="分类管理"/>
    </jsp:include>

</head>
<body>

<div class="grid-container">
    <jsp:include page="/WEB-INF/views/admin/common/navbar.jsp">
        <jsp:param name="active" value="category"/>
    </jsp:include>

    <jsp:include page="/WEB-INF/views/admin/common/header.jsp">
        <jsp:param name="title" value="分类管理"/>
    </jsp:include>

    <main class="p-4">
        <form class="mb-4" action="${pageContext.request.contextPath}/admin/category?method=query" method="post">
            <div class="row">
                <div class="col-md-3">
                    <input type="text" class="form-control" id="id" placeholder="请输入ID" value="${id}"
                           name="id">
                </div>
                <div class="col-md-3">
                    <input type="text" class="form-control" id="name" placeholder="请输入名称"
                           value="${name}" name="name">
                </div>
                <div class="col-md-3">
                    <button type="submit" class="btn btn-primary">查询</button>

                    <a class="btn btn-primary btn-xs"
                       href="${pageContext.request.contextPath}/admin/category?method=viewCreate">新增</a>
                </div>
            </div>
            <!-- 隐藏的分页参数 -->
            <input type="hidden" name="pageNum" value="1">
            <input type="hidden" name="pageSize" value="10">
        </form>

        <table class="table table-bordered table-hover align-middle">
            <thead>
            <tr>
                <td>ID</td>
                <td>名称</td>
                <td>操作</td>
            </tr>
            </thead>
            <tbody>
            <c:forEach items="${list}" var="item">
                <tr>
                    <td>${item.id}</td>
                    <td>${item.name}</td>
                    <td>
                        <div class="action">
                            <a class="btn btn-primary btn-sm"
                               href="${pageContext.request.contextPath}/admin/category?method=viewEdit&id=${item.id}">编辑</a>
                            <a class="btn btn-danger btn-sm"
                               href="${pageContext.request.contextPath}/admin/category?method=remove&id=${item.id}">删除</a>
                        </div>
                    </td>
                </tr>
            </c:forEach>
            </tbody>
        </table>


        <!-- 分页组件 -->
        <c:if test="${pages >= 1}">
            <div class="d-flex justify-content-between align-items-center">
                <div class="page-info text-muted">
                    共 ${total} 条记录，第 ${pageNum}/${pages} 页
                </div>
                <nav>
                    <ul class="pagination">
                        <!-- 上一页 -->
                        <li class="page-item ${pageNum <= 1 ? 'disabled' : ''}">
                            <a class="page-link"
                               href="${pageContext.request.contextPath}/admin/category?method=query&id=${id}&name=${name}&pageNum=${pageNum - 1}&pageSize=${pageSize}"
                                ${pageNum <= 1 ? 'tabindex="-1" aria-disabled="true"' : ''}>
                                上一页
                            </a>
                        </li>

                        <!-- 页码 -->
                        <c:forEach begin="1" end="${pages}" var="i">
                            <c:choose>
                                <c:when test="${i == pageNum}">
                                    <li class="page-item active" aria-current="page">
                                        <span class="page-link">${i}</span>
                                    </li>
                                </c:when>
                                <c:when test="${i >= pageNum - 2 && i <= pageNum + 2}">
                                    <li class="page-item">
                                        <a class="page-link"
                                           href="${pageContext.request.contextPath}/admin/category?method=query&id=${id}&name=${name}&pageNum=${i}&pageSize=${pageSize}">${i}</a>
                                    </li>
                                </c:when>
                                <c:when test="${i == 1 || i == pages}">
                                    <li class="page-item">
                                        <a class="page-link"
                                           href="${pageContext.request.contextPath}/admin/category?method=query&id=${id}&name=${name}&pageNum=${i}&pageSize=${pageSize}">${i}</a>
                                    </li>
                                </c:when>
                                <c:when test="${i == pageNum - 3 || i == pageNum + 3}">
                                    <li class="page-item disabled">
                                        <span class="page-link">...</span>
                                    </li>
                                </c:when>
                            </c:choose>
                        </c:forEach>

                        <!-- 下一页 -->
                        <li class="page-item ${pageNum >= pages ? 'disabled' : ''}">
                            <a class="page-link"
                               href="${pageContext.request.contextPath}/admin/category?method=query&id=${id}&name=${name}&pageNum=${pageNum + 1}&pageSize=${pageSize}"
                                ${pageNum >= pages ? 'tabindex="-1" aria-disabled="true"' : ''}>
                                下一页
                            </a>
                        </li>
                    </ul>
                </nav>
            </div>
        </c:if>
    </main>
</div>

</body>
</html>
